<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAEmail feature of the component
*/

require_once(JPATH_COMPONENT.DS.'asset'.DS.'XMLExportHelper.php');

class JAJobBoardControllerJAXMLExport extends JAJobBoardController {
	
	/**
	 * $_id;
	 * 
	 * */
	var $_id;
	/**
	 * $_tab;
	 * 
	 * */
	var $_tab;
	/**
     * Constructor
     */
    function __construct( $location = array() )
    {
        parent::__construct( $location );  
        $this->_id = JRequest::getVar('cid',  0, '', 'array');
		checkPermission_Administrator('export_job');		
		// Get tab value
		$this->_tab = JRequest::getVar("tab", "schemas"); 
		$this->registerTask("apply", "save");
		$this->registerTask( 'orderup',    'orderdown' );
		$this->registerTask( 'unpublish',    'publish' );
    }
    
    /**
    * Display current japerminission of the component to administrator
    * 
    */
    function display(){
    	$tab = JRequest::getVar("tab", "feeds");
    	$task = JRequest::getVar("task", "list");
    	
    	switch($tab)
		{			
			case "feeds":{
				switch ($task){
					case "list":
						JRequest::setVar("layout", "feeds_list");
						break;
					case "edit":
					case "add":
						JRequest::setVar("layout", "feeds_form");
						JRequest::setVar("hidemainmenu", 1);
						break;
				}
			}
			break;
			case "schemas":{
				switch ($task){
					case "list":
						JRequest::setVar("layout", "schemas_list");
						break;
					case "edit":
					case "add":
						JRequest::setVar("layout", "schemas_form");
						JRequest::setVar("hidemainmenu", 1);
						break;
					case "configxml":
						JRequest::setVar("layout", "schemas_xml");
						JRequest::setVar("hidemainmenu", 1);
						break;
					case "cancel":
						$this->setRedirect("index.php?option=com_jajobboard&view=jaxmlexport&tab=$tab", JText::_("Cancel Operation"));
						break;
				}
			}
			break;
		}
		
		parent::display();
    }
    
    /**
     * Download XML file
     * */
    
    function downloadFeed(){
    	$FeedID = JRequest::getVar("FeedID","0");
    	$FeedName = JRequest::getVar("FeedName","");
    	
		header('Content-type: application/xml; charset=UTF-8');
		
		header('Content-Disposition: attachment; filename="'.$FeedName.'.xml"');
		
		global $jbconfig;
		
		$filename = JPATH_SITE.$jbconfig['general']->get('upload_folder',DS.'images'.DS.'stories'.DS.'jbuploads').DS.$FeedName.".xml";
		
		if (file_exists($filename)) {	
			$fh = fopen($filename, 'rb');
			$contents = fread($fh, filesize($filename));
			fclose($fh);
			echo $contents;
		}
		
		exit();
	}
	
	/**
	 * Clear cache
	 * */
    function clearcache(){
    	$FeedID = JRequest::getVar("FeedID","0");
    	fcnGenerateXMLFile($FeedID);
    	$this->setRedirect("index.php?option=com_jajobboard&view=jaxmlexport&tab=feeds", JText::_("Cache cleared"));
    }
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
    	$tab = JRequest::getVar("tab", "categories");    	
        $this->setRedirect("index.php?option=com_jajobboard&view=jaxmlexport&tab=$tab", JText::_("Cancel Operation"));
    }   
    
    /**
    * Save category record
    * 
    */
    function save(){
    	global $option;
    	switch ($this->_tab){
    		case "schemas":
    			$model =& $this->getModel("jaxmlexport");		
				if ($model->storeSchema()) {
					$msg = JText::_( 'Schema saved!' );
				} else {
					$msg = JText::_( 'Error saving schema' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlexport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    		case "feeds":
    			$model =& $this->getModel("jaxmlexport");		
				if ($model->storeFeed()) {
					$msg = JText::_( 'Feed saved!' );
				} else {
					$msg = JText::_( 'Error saving feed' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlexport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    	}
    } 
    
    /**
    * Remove category record
    * 
    */
    function remove(){
    	global $option;
    	switch ($this->_tab){
    		case "schemas":
    			$model =& $this->getModel("jaxmlexport");		
				if ($model->deleteSchema()) {
					$msg = JText::_( 'Schema deleted!' );
				} else {
					$msg = JText::_( 'Error deleting schema' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlexport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    		case "feeds":
    			$model =& $this->getModel("jaxmlexport");		
				if ($model->deleteItems()) {
					$msg = JText::_( 'Item deleted!' );
				} else {
					$msg = JText::_( 'Error deleting item' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlexport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    	}
    }
}